package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.HisConsumptionDto;
import com.woniu.dto.PageDTO;
import com.woniu.exception.DIYException;
import com.woniu.model.HisConsumption;
import com.woniu.mapper.HisConsumptionMapper;
import com.woniu.param.HisConsumptionAddParam;
import com.woniu.param.HisConsumptionParam;
import com.woniu.param.PageParam;
import com.woniu.service.IHisConsumptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisConsumptionServiceImpl extends ServiceImpl<HisConsumptionMapper, HisConsumption> implements IHisConsumptionService {
    @Resource
    private HisConsumptionMapper hisConsumptionMapper;


    @Override
    public Result<PageDTO<HisConsumptionDto>> getHisConsumptionBy(HisConsumptionParam hisConsumptionParam) {
        if (hisConsumptionParam==null){new DIYException(-9999,"hisConsumptionParam为空");}
        QueryWrapper<HisConsumption> queryWrapper = new QueryWrapper<>();
        if (hisConsumptionParam.getConsumptionId()!=null){
            queryWrapper.eq("consumption_id",hisConsumptionParam.getConsumptionId());
        }
        if (hisConsumptionParam.getPatientId()!=null){
            queryWrapper.eq("patient_id",hisConsumptionParam.getPatientId());
        }
        if (hisConsumptionParam.getPatientName()!=null){
            queryWrapper.eq("patient_name",hisConsumptionParam.getPatientName());
        }
        if (hisConsumptionParam.getConsumptionDesc()!=null){
            queryWrapper.eq("consumption_desc",hisConsumptionParam.getConsumptionDesc());
        }
        if (hisConsumptionParam.getConsumptionPrice()!=null){
            queryWrapper.eq("consumption_price",hisConsumptionParam.getConsumptionPrice());
        }
        if (hisConsumptionParam.getConsumptionStatus()!=null){
            queryWrapper.eq("consumption_status",hisConsumptionParam.getConsumptionStatus());
        }
        if (hisConsumptionParam.getLaborCost()!=null){
            queryWrapper.eq("labor_cost",hisConsumptionParam.getLaborCost());
        }
        if (hisConsumptionParam.getBedFee()!=null){
            queryWrapper.eq("bed_fee",hisConsumptionParam.getBedFee());
        }
        if(hisConsumptionParam.getPageParam()==null){
            PageParam pageParam = new PageParam();
            pageParam.setPage(0L);
            pageParam.setPageSize(100L);
            hisConsumptionParam.setPageParam(pageParam);
        }
        Page<HisConsumption> page = page(new Page<HisConsumption>(hisConsumptionParam.getPageParam().getPage(),hisConsumptionParam.getPageParam().getPageSize()),queryWrapper);
        List<HisConsumption> records = page.getRecords();
        List<HisConsumptionDto> hisConsumptionDtos = CopyBeanUtil.copyBean(records, HisConsumptionDto.class);
        return Result.bulid(PageDTO.bulid(page,hisConsumptionDtos));
    }

    @Override
    public Result<Boolean> deletegetHisConsumptionById(ArrayList<Integer> id) {
        if (id==null) new DIYException(-9999,"服务器异常，id为空");
        int i = hisConsumptionMapper.deleteBatchIds(id);
        return Result.bulid(i==0?new Boolean(false):new Boolean(true));
    }

    @Override
    public Result<HisConsumptionDto> updateHisConsumption(HisConsumptionParam hisConsumptionParam) {
        if (hisConsumptionParam==null){new DIYException(-9999,"住院消费对象为空");}
        HisConsumption hisConsumption = new HisConsumption();
        HisConsumptionDto hisConsumptionDto= new HisConsumptionDto();
        BeanUtils.copyProperties(hisConsumptionParam,hisConsumption);
        BeanUtils.copyProperties(hisConsumptionParam,hisConsumptionDto);
        hisConsumptionMapper.updateById(hisConsumption);
        return Result.bulid(hisConsumptionDto);
    }

    @Override
    public Result<HisConsumptionDto> addHisConsumption(HisConsumptionAddParam hisConsumptionAddParam) {
        if (hisConsumptionAddParam==null) new DIYException(-9999,"服务器异常，hisConsumptionAddParam为空");
        HisConsumption hisConsumption = new HisConsumption();
        BeanUtils.copyProperties(hisConsumptionAddParam,hisConsumption);
        hisConsumptionMapper.insert(hisConsumption);
        HisConsumptionDto hisConsumptionDto = new HisConsumptionDto();
        BeanUtils.copyProperties(hisConsumption,hisConsumptionDto);
        return Result.bulid(hisConsumptionDto);
    }
}
